Imports System.Linq
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.DataAcessLayer.StatementSources
Imports SISSAT.DataAcessLayer.PersistentStorage
Imports SISSAT.BusinessLayer.Utilities

Namespace Services
    Public Class SubjectService
        Inherits ServiceBase
        Implements ISubjectService

        Public Sub Create(code As String, description As String, name As String, units As Decimal, createdBy As Integer) Implements ISubjectService.Create
            StatementSource = New CreateSubjectStatementSource(code, description, name, units, createdBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Delete(subjectId As Integer) Implements ISubjectService.Delete
            StatementSource = New DeleteSubjectStatementSource(subjectId)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Edit(entity As Subject, modifiedBy As Integer) Implements ISubjectService.Edit
            StatementSource = _
                New UpdateSubjectStatementSource(entity.SubjectId, entity.Code, entity.Description, entity.Name, entity.Units, modifiedBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Function ReadAll() As IEnumerable(Of Subject) Implements ISubjectService.ReadAll
            StatementSource = New ReadAllSubjectStatementSource()
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return DataMapper.MapDataToEntity(Of Subject)(dataReader)
        End Function

        Public Function ReadById(subjectId As Integer) As Subject Implements ISubjectService.ReadById
            StatementSource = New ReadBySubjectIdStatementSource(subjectId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return DataMapper.MapDataToEntity(Of Subject)(dataReader).FirstOrDefault()
        End Function

    End Class
End Namespace